import Vue from 'vue'
import vuex from 'vuex'
Vue.use(vuex);
import state from './state'
export default new vuex.Store({
    state:state,
    mutations: {
        addToken (state,token) {
            state.token = token
        },
        symbolData (state, data) {
            state.tradeData = data
        },
        newPrice (state, price){
            state.nowPrice = price
        },
        rate (state,rate){
            state.rate = rate
        },
        add (state, number) {
            state.number += number
        }
    },
    getters:{//类似vue的computed
        getToken:function(state){
            let token = state.token;
            if(!token || token==""){//如果 state 里面获取不到，那么从localStorage里面获取
                token = window.localStorage.getItem('token') || '';
            }
            return token;
        },
        getNumber:function(state){//上面的state
            return state.number-1;
        }

    },
    actions:{//类似vue的methods
        changeToken(context,token){
            context.commit("addToken",token);
        },
        tradeChange(context,data){//接收一个与store实例具有相同方法的属性的context对象
            context.commit("symbolData",data);
        },
        priceChange(context,price){
            context.commit("newPrice",price);
        },
        rateChange(context,rate){
            context.commit("rate",rate);
        },
        addFun(context,number){
            context.commit("add",number);
        }
    }
})
